package com.labot.authentication.encryp;

import javax.crypto.Cipher;

import org.apache.commons.codec.binary.Base64;


public class EncryptDecryptUtils {

	public static String encrypt(String value) {
	    try {

	    	CustomSecutityKeyImpl skeySpec = new CustomSecutityKeyImpl();
	        Cipher cipher = Cipher.getInstance("DESede");
	        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
	        byte[] encrypted = cipher.doFinal(value.getBytes("ISO-8859-1"));
	        return Base64.encodeBase64String(encrypted);
	    } catch (Exception ex) {
	        ex.printStackTrace();
	    }
	    return null;
	}

	public static String decrypt(String encrypted) {
	    try {
	    	CustomSecutityKeyImpl skeySpec = new CustomSecutityKeyImpl();
	        Cipher cipher = Cipher.getInstance("DESede");
	        cipher.init(Cipher.DECRYPT_MODE, skeySpec);
	        byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));

	        return new String(original);
	    } catch (Exception ex) {
	        ex.printStackTrace();
	    }
	    return null;
	}
	
	public static String encrypt(String value, String key) {
	    try {

	    	HashSecutityKeyImpl skeySpec = new HashSecutityKeyImpl();
	    	skeySpec.setHash(key);
	        Cipher cipher = Cipher.getInstance("DESede");
	        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
	        byte[] encrypted = cipher.doFinal(value.getBytes("ISO-8859-1"));
	        return Base64.encodeBase64String(encrypted);
	    } catch (Exception ex) {
	        ex.printStackTrace();
	    }
	    return null;
	}

	public static String decrypt(String encrypted, String key) {
	    try {
	    	HashSecutityKeyImpl skeySpec = new HashSecutityKeyImpl();
	    	skeySpec.setHash(key);
	        Cipher cipher = Cipher.getInstance("DESede");
	        cipher.init(Cipher.DECRYPT_MODE, skeySpec);
	        byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));

	        return new String(original);
	    } catch (Exception ex) {
	        ex.printStackTrace();
	    }
	    return null;
	}
}